import requests
from www.headers1 import headers
from bs4 import BeautifulSoup
import time


def get_info(url):
    temp_dict = {}
    if 'https://jobs.51job.com' in url:
        r = requests.get(url, headers=headers)
        time.sleep(1.5)
        soup = BeautifulSoup(r.content.decode('gbk'), 'html5lib')
        temp_dict['job_id'] = url.split('.html')[0].split('/')[-1]
        temp_dict['company_name'] = soup.find('a', class_='catn').getText().strip()
        com_tag = soup.find('div', class_='com_tag').find_all('p')
        for i in com_tag:
            if 'i_flag' in str(i):
                temp_dict['company_type'] = i.getText()
            if 'i_people' in str(i):
                temp_dict['company_scale'] = i.getText()
            if 'i_trade' in str(i):
                tempsoup = BeautifulSoup(str(i), 'html5lib')
                bb = []
                for i in tempsoup.find_all('a'):
                    bb.append(i.getText().strip())
                temp_dict['company_trade'] = '/'.join(bb)
        temp_dict['job_name'] = soup.find('h1').getText().strip()
        temp_dict['job_pay'] = soup.find('div', class_='cn').find('strong').getText().strip()
        msgltype = soup.find('p', class_='msg ltype').getText().split('|')
        education = ['初中', '高中', '中技', '中专', '大专', '高中', '本科', '硕士', '博士']
        if msgltype:
            for i in msgltype:
                if '经验' in i:
                    temp_dict['job_years'] = i.strip()
                elif '人' in i:
                    temp_dict['job_member'] = i.strip()
                elif '发布' in i:
                    temp_dict['job_date'] = i.strip()
                elif i in education:
                    temp_dict['job_education'] = i.strip()
        t1 = soup.find('div', class_='t1').find_all('span')
        welfare = []
        for i in t1:
            welfare.append(i.getText().strip())
        temp_dict['company_welfare'] = '/'.join(welfare)
        bmsg = soup.find('div', class_='bmsg inbox')  # 上班地点
        if bmsg:
            if bmsg.find('p', class_='fp'):
                temp_dict['job_location'] = bmsg.find('p', class_='fp').getText().strip()
        find_decribe = soup.find('div', class_='bmsg job_msg inbox')
        temp = str(find_decribe).split('<div class="mt10">')[0]
        mysoup = BeautifulSoup(temp, 'html5lib')
        temp_dict['job_describe'] = mysoup.getText().strip()
        temp_dict['recuit_sources'] = '前程无忧'
        return temp_dict